
const router = require('koa-router')();
const util = require("../common/utils")
const { sequelize } = require('../common/dbs')

const commontModel = require("../models/commont")
const commontinfo = commontModel(sequelize);

const commont_hfModel = require("../models/commont_hf")
const commont_hfinfo = commont_hfModel(sequelize);


//最新评论
router.get('/news', async (ctx) => {
    try {
        // foreignKey 目标表中外键的名称
        // sourceKey 源表中关联的键的字段的名称。默认为源表的主键。
        commontinfo.hasMany(commont_hfinfo, { foreignKey: 'levid', sourceKey: 'commontid' });
        let reqQuery = ctx.request.query;
        let commontData = await commontinfo.findAndCountAll(
            {
                attributes: {   // exclude排除
                    exclude: ['commontshow'],
                },
                order: [  //排序 DESC ASC
                    ['createdAt', 'DESC']
                ],
                where: {
                    commontshow: 1
                },
                include: [
                    {
                        attributes: {   // exclude排除
                            exclude: ['commontshow'],
                        },
                        model: commont_hfinfo,
                    }
                ],
            }
        );

        ctx.body = util.resultSuccess({ data: commontData });
    } catch (err) {
        console.log(err);
        ctx.body = util.resultError({ data: err });
    }
})

// 分页查询
router.get('/list', async (ctx) => {
    try {
        // foreignKey 目标表中外键的名称
        // sourceKey 源表中关联的键的字段的名称。默认为源表的主键。
        commontinfo.hasMany(commont_hfinfo, { foreignKey: 'levid', sourceKey: 'commontid' });
        let reqQuery = ctx.request.query;
        let commontData = await commontinfo.findAndCountAll(
            {
                attributes: {   // exclude排除
                    exclude: ['commontshow'],
                },
                where: {
                    cardid: reqQuery.cardid,
                    commontshow: 1
                },
                include: [
                    {
                        attributes: {   // exclude排除
                            exclude: ['commontshow'],
                        },
                        model: commont_hfinfo,
                    }
                ],
            }
        );

        ctx.body = util.resultSuccess({ data: commontData });
    } catch (err) {
        console.log(err);
        ctx.body = util.resultError({ data: err });
    }
})
// 添加评论
router.post('/add', async (ctx) => {
    try {
        ctx
            .validateBody("username")
            .required("必须填写昵称")
            .isString()
            .trim()
        ctx
            .validateBody("cardcontent")
            .required("评论不能为空")
            .isString()
            .trim()
        let reqBody = ctx.request.body;
        let commont = {
            username: reqBody.username,
            commontc: reqBody.cardcontent,
            useremail: reqBody.email,
            userurl: reqBody.url,
            cardid: reqBody.cardid,
            ua: reqBody.ua,
            // toid: reqBody.toid,
            // toEmail: reqBody.toEmail ? reqBody.toEmail : '',
            // toName: reqBody.toName ? reqBody.toName : '',
            // toContent: reqBody.toContent ? reqBody.toContent : '',
        };
        // toName 如果有数据就是二级评论
        let requestData = null;
        if (reqBody.toid) {
            commont['levid'] = reqBody.toid;
            commont['toname'] = reqBody.toName;

            requestData = await commont_hfinfo.create(commont)
        } else {
            requestData = await commontinfo.create(commont)
        }

        if (requestData.dataValues) {
            ctx.body = util.resultSuccess({ message: '添加成功', data: requestData.dataValues });
        } else {
            ctx.body = util.resultSuccess({ code: 0, message: '添加失败', data: null });
        }
    } catch (err) {
        console.log(err);
        ctx.body = util.resultError({ data: err });
    }
})


module.exports = router.routes(); 